using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using HslCommunication.Profinet.Siemens;
using WIT.DAQ.Core.Brands.OMRON;
using WIT.DAQ.Core.Enums;
using WIT.DAQ.Core.Forms.UserControls;
using WIT.DAQ.Core.Models;

namespace WIT.DAQ.Core.Forms;

/// <summary>
///
/// </summary>
internal class DeviceEditForm : BaseForm
{
	/// <summary>
	///
	/// </summary>
	/// <param name="device"></param>
	internal delegate void SaveDeviceHandler(Device device);

	/// <summary>
	/// 实例化的目的
	/// </summary>
	private readonly EnumFormPurpose PURPOSE;

	/// <summary>
	/// 当次编辑的设备
	/// </summary>
	private readonly Device DEVICE;

	/// <summary>
	/// 记录当前选择的通信协议
	/// </summary>
	private EnumProtocol PROTOCOL;

	private const string TITLE_ADD = "添加设备";

	private const string TITLE_EDIT = "编辑设备";

	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private Button btnCancel;

	private Button btnConfirm;

	private Label label1;

	private ComboBox cmbDeviceType;

	private Label label2;

	private ComboBox cmbBrand;

	private Label label3;

	private ComboBox cmbModel;

	private TextBox txtName;

	private Label label4;

	private Label label5;

	private TextBox txtDesc;

	private Label label6;

	private Label label7;

	private Label label8;

	private Label label9;

	private GroupBox grpParams;

	private Panel pnlContainer;

	private RadioButton rbOPCUA;

	private RadioButton rbS7;

	private RadioButton rbModbusTcp;

	private GroupBox grpProtocols;

	private Label label10;

	private Label label11;

	private Label label12;

	private TextBox txtPort;

	private TextBox txtIP;

	private Label label13;

	private Label label14;

	private RadioButton rbSocket;

	private RadioButton rbModbusRtu;

	/// <summary>
	///
	/// </summary>
	internal event SaveDeviceHandler SaveDevice;

	/// <summary>
	///
	/// </summary>
	/// <param name="purpose">目的</param>
	/// <param name="device">编辑时传入设备</param>
	internal DeviceEditForm(EnumFormPurpose purpose, Device device = null)
	{
		InitializeComponent();
		rbOPCUA.Tag = EnumProtocol.OPCUA;
		rbS7.Tag = EnumProtocol.S7;
		rbModbusTcp.Tag = EnumProtocol.ModbusTcp;
		rbModbusRtu.Tag = EnumProtocol.ModbusRtu;
		rbSocket.Tag = EnumProtocol.Socket;
		PURPOSE = purpose;
		DEVICE = device;
		InitDeviceTypes();
		InitBrands();
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			Text = "添加设备";
			break;
		case EnumFormPurpose.Edit:
			Text = "编辑设备";
			txtName.Text = DEVICE.Name;
			cmbDeviceType.SelectedValue = DEVICE.Type;
			cmbBrand.SelectedValue = DEVICE.Brand;
			cmbModel.SelectedValue = DEVICE.Model;
			txtIP.Text = DEVICE.IP;
			txtPort.Text = DEVICE.Port.ToString();
			txtDesc.Text = DEVICE.Desc;
			break;
		}
	}

	private void DeviceEditForm_Load(object sender, EventArgs e)
	{
		ToolTip toolTip = new ToolTip();
		toolTip.AutoPopDelay = 3000;
		toolTip.InitialDelay = 500;
		toolTip.ReshowDelay = 500;
		toolTip.ShowAlways = true;
		toolTip.SetToolTip(rbOPCUA, "通信速度：中\r稳定性：中");
		ToolTip toolTip2 = new ToolTip();
		toolTip2.AutoPopDelay = 3000;
		toolTip2.InitialDelay = 500;
		toolTip2.ReshowDelay = 500;
		toolTip2.ShowAlways = true;
		toolTip2.SetToolTip(rbS7, "通信速度：快\r稳定性：高");
		ToolTip toolTip3 = new ToolTip();
		toolTip3.AutoPopDelay = 3000;
		toolTip3.InitialDelay = 500;
		toolTip3.ReshowDelay = 500;
		toolTip3.ShowAlways = true;
		toolTip3.SetToolTip(rbModbusTcp, "通信速度：快\r稳定性：高");
		ToolTip toolTip4 = new ToolTip();
		toolTip4.AutoPopDelay = 3000;
		toolTip4.InitialDelay = 500;
		toolTip4.ReshowDelay = 500;
		toolTip4.ShowAlways = true;
		toolTip4.SetToolTip(rbModbusRtu, "通信速度：慢\r稳定性：中");
		ToolTip toolTip5 = new ToolTip();
		toolTip5.AutoPopDelay = 3000;
		toolTip5.InitialDelay = 500;
		toolTip5.ReshowDelay = 500;
		toolTip5.ShowAlways = true;
		toolTip5.SetToolTip(rbSocket, "通信速度：极快\r稳定性：高");
		txtName.Select();
	}

	/// <summary>
	/// 加载设备类型
	/// </summary>
	private void InitDeviceTypes()
	{
		List<object> list = new List<object>();
		foreach (EnumDeviceType value in Enum.GetValues(typeof(EnumDeviceType)))
		{
			list.Add(new
			{
				Name = value.ToString(),
				Value = (int)value
			});
		}
		cmbDeviceType.DisplayMember = "Name";
		cmbDeviceType.ValueMember = "Value";
		cmbDeviceType.DataSource = list;
	}

	/// <summary>
	/// 加载品牌
	/// </summary>
	private void InitBrands()
	{
		List<object> list = new List<object>();
		foreach (object value in Enum.GetValues(typeof(EnumBrand)))
		{
			list.Add(new
			{
				Name = value.ToString(),
				Value = (int)value
			});
		}
		cmbBrand.DisplayMember = "Name";
		cmbBrand.ValueMember = "Value";
		cmbBrand.DataSource = list;
	}

	/// <summary>
	/// 联动加载型号
	/// </summary>
	private void InitModels_Coupling(object sender, EventArgs e)
	{
		switch ((EnumDeviceType)cmbDeviceType.SelectedValue)
		{
		case EnumDeviceType.PLC:
			switch ((EnumBrand)cmbBrand.SelectedValue)
			{
			case EnumBrand.SIEMENS:
			{
				List<object> list3 = new List<object>();
				foreach (object value in Enum.GetValues(typeof(SiemensPLCS)))
				{
					list3.Add(new
					{
						Name = value.ToString(),
						Value = (int)value
					});
				}
				cmbModel.DisplayMember = "Name";
				cmbModel.ValueMember = "Value";
				cmbModel.DataSource = list3;
				break;
			}
			case EnumBrand.OMRON:
			{
				List<object> list4 = new List<object>();
				foreach (object value2 in Enum.GetValues(typeof(PLCModel)))
				{
					list4.Add(new
					{
						Name = value2.ToString(),
						Value = (int)value2
					});
				}
				cmbModel.DisplayMember = "Name";
				cmbModel.ValueMember = "Value";
				cmbModel.DataSource = list4;
				break;
			}
			case EnumBrand.Other:
			{
				List<object> list2 = new List<object>();
				foreach (object value3 in Enum.GetValues(typeof(EnumOtherDeviceModel)))
				{
					list2.Add(new
					{
						Name = value3.ToString(),
						Value = (int)value3
					});
				}
				cmbModel.DisplayMember = "Name";
				cmbModel.ValueMember = "Value";
				cmbModel.DataSource = list2;
				break;
			}
			default:
				throw new Exception("联动加载型号时发生未知错误");
			}
			break;
		case EnumDeviceType.Other:
		{
			List<object> list = new List<object>();
			foreach (object value4 in Enum.GetValues(typeof(EnumOtherDeviceModel)))
			{
				list.Add(new
				{
					Name = value4.ToString(),
					Value = (int)value4
				});
			}
			cmbModel.DisplayMember = "Name";
			cmbModel.ValueMember = "Value";
			cmbModel.DataSource = list;
			break;
		}
		default:
			throw new Exception("联动加载品牌时发生未知错误");
		}
	}

	/// <summary>
	/// 型号下拉框选择项改变时，判断通信协议
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void cmbModel_SelectedIndexChanged(object sender, EventArgs e)
	{
		if (cmbModel.SelectedValue == null)
		{
			return;
		}
		List<RadioButton> list = new List<RadioButton>();
		foreach (object control in grpProtocols.Controls)
		{
			if (control.GetType() == typeof(RadioButton))
			{
				RadioButton radioButton = (RadioButton)control;
				radioButton.Enabled = false;
				list.Add(radioButton);
			}
		}
		switch ((EnumDeviceType)cmbDeviceType.SelectedValue)
		{
		case EnumDeviceType.PLC:
			switch ((EnumBrand)cmbBrand.SelectedValue)
			{
			case EnumBrand.SIEMENS:
				switch ((SiemensPLCS)cmbModel.SelectedValue)
				{
				case SiemensPLCS.S1500:
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.OPCUA).Enabled = true;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.S7).Enabled = true;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusTcp).Enabled = true;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusRtu).Enabled = false;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.Socket).Enabled = true;
					if (PURPOSE == EnumFormPurpose.Edit && list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Enabled)
					{
						list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Checked = true;
					}
					else
					{
						list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.OPCUA).Checked = true;
					}
					break;
				case SiemensPLCS.S1200:
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.OPCUA).Enabled = true;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.S7).Enabled = true;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusTcp).Enabled = true;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusRtu).Enabled = false;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.Socket).Enabled = true;
					if (PURPOSE == EnumFormPurpose.Edit && list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Enabled)
					{
						list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Checked = true;
					}
					else
					{
						list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.S7).Checked = true;
					}
					break;
				default:
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.OPCUA).Enabled = false;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.S7).Enabled = true;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusTcp).Enabled = true;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusRtu).Enabled = false;
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.Socket).Enabled = true;
					if (PURPOSE == EnumFormPurpose.Edit && list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Enabled)
					{
						list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Checked = true;
					}
					else
					{
						list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.S7).Checked = true;
					}
					break;
				}
				break;
			case EnumBrand.OMRON:
				list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.OPCUA).Enabled = true;
				list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.S7).Enabled = false;
				list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusTcp).Enabled = true;
				list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusRtu).Enabled = false;
				list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.Socket).Enabled = true;
				if (PURPOSE == EnumFormPurpose.Edit && list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Enabled)
				{
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Checked = true;
				}
				else
				{
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.OPCUA).Checked = true;
				}
				break;
			case EnumBrand.Other:
				foreach (RadioButton item in list)
				{
					item.Enabled = true;
				}
				list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusTcp).Checked = true;
				if (PURPOSE == EnumFormPurpose.Edit)
				{
					list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Checked = true;
				}
				break;
			default:
				throw new Exception("联动加载型号时发生未知错误");
			}
			break;
		case EnumDeviceType.Other:
			foreach (RadioButton item2 in list)
			{
				item2.Enabled = true;
			}
			list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == EnumProtocol.ModbusTcp).Checked = true;
			if (PURPOSE == EnumFormPurpose.Edit)
			{
				list.FirstOrDefault((RadioButton t) => (EnumProtocol)t.Tag == DEVICE.Protocol).Checked = true;
			}
			break;
		default:
			throw new Exception("联动加载品牌时发生未知错误");
		}
	}

	/// <summary>
	/// 根据通信协议加载设备参数UserControl
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void RadioButton_CheckedChanged(object sender, EventArgs e)
	{
		RadioButton radioButton = sender as RadioButton;
		if (!radioButton.Checked)
		{
			return;
		}
		pnlContainer.Controls.Clear();
		switch ((EnumProtocol)radioButton.Tag)
		{
		case EnumProtocol.OPCUA:
			if (PURPOSE == EnumFormPurpose.Edit)
			{
				pnlContainer.Controls.Add(new UC_DeviceParams_For_OPCUA(DEVICE.Suffix, DEVICE.NeedAccount, DEVICE.Account, DEVICE.Pwd));
				break;
			}
			txtPort.Text = "4840";
			pnlContainer.Controls.Add(new UC_DeviceParams_For_OPCUA());
			break;
		case EnumProtocol.S7:
			if (PURPOSE == EnumFormPurpose.Edit)
			{
				pnlContainer.Controls.Add(new UC_DeviceParams_For_S7(DEVICE.Rack, DEVICE.Slot));
				break;
			}
			txtPort.Text = "102";
			pnlContainer.Controls.Add(new UC_DeviceParams_For_S7());
			break;
		case EnumProtocol.ModbusTcp:
			if (PURPOSE == EnumFormPurpose.Edit)
			{
				pnlContainer.Controls.Add(new UC_DeviceParams_For_ModbusTcp(DEVICE.DataFormat, DEVICE.AddressStartWithZero, DEVICE.IsStringReverse, DEVICE.Station));
				break;
			}
			txtPort.Text = "502";
			pnlContainer.Controls.Add(new UC_DeviceParams_For_ModbusTcp());
			break;
		case EnumProtocol.ModbusRtu:
			if (PURPOSE == EnumFormPurpose.Edit)
			{
				pnlContainer.Controls.Add(new UC_DeviceParams_For_ModbusRtu(DEVICE.DataFormat, DEVICE.AddressStartWithZero, DEVICE.IsStringReverse, DEVICE.Station, DEVICE.ComPort, DEVICE.BaudRate, DEVICE.DataBits, DEVICE.StopBits, DEVICE.Parity));
				break;
			}
			txtIP.Text = "127.0.0.1";
			txtPort.Text = "-1";
			pnlContainer.Controls.Add(new UC_DeviceParams_For_ModbusRtu());
			break;
		case EnumProtocol.Socket:
			if (PURPOSE != EnumFormPurpose.Edit)
			{
				txtPort.Text = string.Empty;
			}
			break;
		default:
			throw new Exception($"未知的通信协议[{radioButton.Tag}]");
		}
		PROTOCOL = (EnumProtocol)radioButton.Tag;
	}

	/// <summary>
	/// 确认
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnConfirm_Click(object sender, EventArgs e)
	{
		if (string.IsNullOrEmpty(txtName.Text.Trim()))
		{
			MessageBox.Show("请输入设备自定义名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		if (cmbDeviceType.SelectedItem == null)
		{
			MessageBox.Show("请选择设备类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		if (cmbBrand.SelectedItem == null)
		{
			MessageBox.Show("请选择设备品牌", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		if (cmbModel.SelectedItem == null)
		{
			MessageBox.Show("请选择设备型号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		if (!IPAddress.TryParse(txtIP.Text.Trim(), out var address))
		{
			MessageBox.Show("IP格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		if (!int.TryParse(txtPort.Text.Trim(), out var result))
		{
			MessageBox.Show("端口号格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		Device device = null;
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			device = new Device
			{
				Id = Guid.NewGuid(),
				Name = txtName.Text.Trim(),
				Type = Convert.ToInt32(cmbDeviceType.SelectedValue),
				Brand = Convert.ToInt32(cmbBrand.SelectedValue),
				Model = Convert.ToInt32(cmbModel.SelectedValue),
				Protocol = PROTOCOL,
				IP = address.ToString(),
				Port = result,
				Desc = txtDesc.Text.Trim()
			};
			break;
		case EnumFormPurpose.Edit:
			device = DEVICE;
			device.Name = txtName.Text.Trim();
			device.Type = Convert.ToInt32(cmbDeviceType.SelectedValue);
			device.Brand = Convert.ToInt32(cmbBrand.SelectedValue);
			device.Model = Convert.ToInt32(cmbModel.SelectedValue);
			device.Protocol = PROTOCOL;
			device.IP = address.ToString();
			device.Port = result;
			device.Desc = txtDesc.Text.Trim();
			break;
		}
		device.Suffix = null;
		device.NeedAccount = null;
		device.Account = null;
		device.Pwd = null;
		device.Rack = null;
		device.Slot = null;
		device.DataFormat = null;
		device.AddressStartWithZero = null;
		device.IsStringReverse = null;
		device.Station = null;
		device.ComPort = null;
		device.BaudRate = null;
		device.DataBits = null;
		device.StopBits = null;
		device.Parity = null;
		switch (PROTOCOL)
		{
		case EnumProtocol.OPCUA:
		{
			UC_DeviceParams_For_OPCUA uC_DeviceParams_For_OPCUA = (UC_DeviceParams_For_OPCUA)pnlContainer.Controls[0];
			if (!uC_DeviceParams_For_OPCUA.ValidateInputs(out var msg3))
			{
				MessageBox.Show(msg3, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			device.Suffix = uC_DeviceParams_For_OPCUA.SUFFIX;
			device.NeedAccount = uC_DeviceParams_For_OPCUA.NEEDACCOUNT;
			device.Account = uC_DeviceParams_For_OPCUA.ACCOUNT;
			device.Pwd = uC_DeviceParams_For_OPCUA.PWD;
			break;
		}
		case EnumProtocol.S7:
		{
			UC_DeviceParams_For_S7 uC_DeviceParams_For_S = (UC_DeviceParams_For_S7)pnlContainer.Controls[0];
			if (!uC_DeviceParams_For_S.ValidateInputs(out var msg2))
			{
				MessageBox.Show(msg2, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			device.Rack = uC_DeviceParams_For_S.RACK;
			device.Slot = uC_DeviceParams_For_S.SLOT;
			break;
		}
		case EnumProtocol.ModbusTcp:
		{
			UC_DeviceParams_For_ModbusTcp uC_DeviceParams_For_ModbusTcp = (UC_DeviceParams_For_ModbusTcp)pnlContainer.Controls[0];
			if (!uC_DeviceParams_For_ModbusTcp.ValidateInputs(out var msg4))
			{
				MessageBox.Show(msg4, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			device.DataFormat = uC_DeviceParams_For_ModbusTcp.DATAFORMAT;
			device.AddressStartWithZero = uC_DeviceParams_For_ModbusTcp.ADDRESSSTARTWITHZERO;
			device.IsStringReverse = uC_DeviceParams_For_ModbusTcp.ISSTRINGREVERSE;
			device.Station = uC_DeviceParams_For_ModbusTcp.STATION;
			break;
		}
		case EnumProtocol.ModbusRtu:
		{
			UC_DeviceParams_For_ModbusRtu uC_DeviceParams_For_ModbusRtu = (UC_DeviceParams_For_ModbusRtu)pnlContainer.Controls[0];
			if (!uC_DeviceParams_For_ModbusRtu.ValidateInputs(out var msg))
			{
				MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			device.DataFormat = uC_DeviceParams_For_ModbusRtu.DATAFORMAT;
			device.AddressStartWithZero = uC_DeviceParams_For_ModbusRtu.ADDRESSSTARTWITHZERO;
			device.IsStringReverse = uC_DeviceParams_For_ModbusRtu.ISSTRINGREVERSE;
			device.Station = uC_DeviceParams_For_ModbusRtu.STATION;
			device.ComPort = uC_DeviceParams_For_ModbusRtu.COMPORT;
			device.BaudRate = uC_DeviceParams_For_ModbusRtu.BAUDRATE;
			device.DataBits = uC_DeviceParams_For_ModbusRtu.DATABITS;
			device.StopBits = uC_DeviceParams_For_ModbusRtu.STOPBITS;
			device.Parity = uC_DeviceParams_For_ModbusRtu.PARITY;
			break;
		}
		default:
			throw new Exception($"未知的通信协议[{PROTOCOL}]");
		case EnumProtocol.Socket:
			break;
		}
		this.SaveDevice(device);
		Close();
	}

	/// <summary>
	/// 关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	/// <summary>
	/// ESC关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void DeviceEditForm_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Escape)
		{
			btnCancel_Click(null, null);
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnConfirm = new System.Windows.Forms.Button();
		this.label1 = new System.Windows.Forms.Label();
		this.cmbDeviceType = new System.Windows.Forms.ComboBox();
		this.label2 = new System.Windows.Forms.Label();
		this.cmbBrand = new System.Windows.Forms.ComboBox();
		this.label3 = new System.Windows.Forms.Label();
		this.cmbModel = new System.Windows.Forms.ComboBox();
		this.txtName = new System.Windows.Forms.TextBox();
		this.label4 = new System.Windows.Forms.Label();
		this.label5 = new System.Windows.Forms.Label();
		this.txtDesc = new System.Windows.Forms.TextBox();
		this.label6 = new System.Windows.Forms.Label();
		this.label7 = new System.Windows.Forms.Label();
		this.label8 = new System.Windows.Forms.Label();
		this.label9 = new System.Windows.Forms.Label();
		this.grpParams = new System.Windows.Forms.GroupBox();
		this.pnlContainer = new System.Windows.Forms.Panel();
		this.rbOPCUA = new System.Windows.Forms.RadioButton();
		this.rbS7 = new System.Windows.Forms.RadioButton();
		this.rbModbusTcp = new System.Windows.Forms.RadioButton();
		this.grpProtocols = new System.Windows.Forms.GroupBox();
		this.rbSocket = new System.Windows.Forms.RadioButton();
		this.rbModbusRtu = new System.Windows.Forms.RadioButton();
		this.label10 = new System.Windows.Forms.Label();
		this.label11 = new System.Windows.Forms.Label();
		this.label12 = new System.Windows.Forms.Label();
		this.txtPort = new System.Windows.Forms.TextBox();
		this.txtIP = new System.Windows.Forms.TextBox();
		this.label13 = new System.Windows.Forms.Label();
		this.label14 = new System.Windows.Forms.Label();
		this.grpParams.SuspendLayout();
		this.grpProtocols.SuspendLayout();
		base.SuspendLayout();
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.Location = new System.Drawing.Point(472, 526);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 10;
		this.btnCancel.Text = "取消(&C)";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnConfirm.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnConfirm.Location = new System.Drawing.Point(391, 526);
		this.btnConfirm.Name = "btnConfirm";
		this.btnConfirm.Size = new System.Drawing.Size(75, 23);
		this.btnConfirm.TabIndex = 9;
		this.btnConfirm.Text = "确认(&S)";
		this.btnConfirm.UseVisualStyleBackColor = true;
		this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(22, 66);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(65, 12);
		this.label1.TabIndex = 2;
		this.label1.Text = "设备类型：";
		this.cmbDeviceType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbDeviceType.FormattingEnabled = true;
		this.cmbDeviceType.Location = new System.Drawing.Point(93, 63);
		this.cmbDeviceType.Name = "cmbDeviceType";
		this.cmbDeviceType.Size = new System.Drawing.Size(121, 20);
		this.cmbDeviceType.TabIndex = 1;
		this.cmbDeviceType.SelectedIndexChanged += new System.EventHandler(InitModels_Coupling);
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(22, 92);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(65, 12);
		this.label2.TabIndex = 2;
		this.label2.Text = "品    牌：";
		this.cmbBrand.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbBrand.FormattingEnabled = true;
		this.cmbBrand.Location = new System.Drawing.Point(93, 89);
		this.cmbBrand.Name = "cmbBrand";
		this.cmbBrand.Size = new System.Drawing.Size(121, 20);
		this.cmbBrand.TabIndex = 2;
		this.cmbBrand.SelectedIndexChanged += new System.EventHandler(InitModels_Coupling);
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(22, 118);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(65, 12);
		this.label3.TabIndex = 2;
		this.label3.Text = "型    号：";
		this.cmbModel.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbModel.FormattingEnabled = true;
		this.cmbModel.Location = new System.Drawing.Point(93, 115);
		this.cmbModel.Name = "cmbModel";
		this.cmbModel.Size = new System.Drawing.Size(121, 20);
		this.cmbModel.TabIndex = 3;
		this.cmbModel.SelectedIndexChanged += new System.EventHandler(cmbModel_SelectedIndexChanged);
		this.txtName.Location = new System.Drawing.Point(93, 36);
		this.txtName.MaxLength = 20;
		this.txtName.Name = "txtName";
		this.txtName.Size = new System.Drawing.Size(200, 21);
		this.txtName.TabIndex = 0;
		this.label4.AutoSize = true;
		this.label4.Location = new System.Drawing.Point(22, 39);
		this.label4.Name = "label4";
		this.label4.Size = new System.Drawing.Size(65, 12);
		this.label4.TabIndex = 2;
		this.label4.Text = "设备名称：";
		this.label5.AutoSize = true;
		this.label5.ForeColor = System.Drawing.Color.Red;
		this.label5.Location = new System.Drawing.Point(299, 39);
		this.label5.Name = "label5";
		this.label5.Size = new System.Drawing.Size(113, 12);
		this.label5.TabIndex = 2;
		this.label5.Text = "* 设备的自定义名称";
		this.txtDesc.Location = new System.Drawing.Point(93, 240);
		this.txtDesc.Multiline = true;
		this.txtDesc.Name = "txtDesc";
		this.txtDesc.Size = new System.Drawing.Size(371, 87);
		this.txtDesc.TabIndex = 7;
		this.label6.AutoSize = true;
		this.label6.Location = new System.Drawing.Point(24, 243);
		this.label6.Name = "label6";
		this.label6.Size = new System.Drawing.Size(65, 12);
		this.label6.TabIndex = 2;
		this.label6.Text = "描    述：";
		this.label7.AutoSize = true;
		this.label7.ForeColor = System.Drawing.Color.Red;
		this.label7.Location = new System.Drawing.Point(220, 66);
		this.label7.Name = "label7";
		this.label7.Size = new System.Drawing.Size(17, 12);
		this.label7.TabIndex = 2;
		this.label7.Text = "* ";
		this.label8.AutoSize = true;
		this.label8.ForeColor = System.Drawing.Color.Red;
		this.label8.Location = new System.Drawing.Point(220, 92);
		this.label8.Name = "label8";
		this.label8.Size = new System.Drawing.Size(17, 12);
		this.label8.TabIndex = 2;
		this.label8.Text = "* ";
		this.label9.AutoSize = true;
		this.label9.ForeColor = System.Drawing.Color.Red;
		this.label9.Location = new System.Drawing.Point(220, 118);
		this.label9.Name = "label9";
		this.label9.Size = new System.Drawing.Size(17, 12);
		this.label9.TabIndex = 2;
		this.label9.Text = "* ";
		this.grpParams.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.grpParams.Controls.Add(this.pnlContainer);
		this.grpParams.Location = new System.Drawing.Point(12, 333);
		this.grpParams.Name = "grpParams";
		this.grpParams.Size = new System.Drawing.Size(535, 187);
		this.grpParams.TabIndex = 8;
		this.grpParams.TabStop = false;
		this.grpParams.Text = "设备参数";
		this.pnlContainer.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.pnlContainer.Location = new System.Drawing.Point(6, 16);
		this.pnlContainer.Name = "pnlContainer";
		this.pnlContainer.Size = new System.Drawing.Size(523, 165);
		this.pnlContainer.TabIndex = 0;
		this.rbOPCUA.AutoSize = true;
		this.rbOPCUA.Location = new System.Drawing.Point(6, 18);
		this.rbOPCUA.Name = "rbOPCUA";
		this.rbOPCUA.Size = new System.Drawing.Size(53, 16);
		this.rbOPCUA.TabIndex = 0;
		this.rbOPCUA.TabStop = true;
		this.rbOPCUA.Text = "OPCUA";
		this.rbOPCUA.UseVisualStyleBackColor = true;
		this.rbOPCUA.CheckedChanged += new System.EventHandler(RadioButton_CheckedChanged);
		this.rbS7.AutoSize = true;
		this.rbS7.Location = new System.Drawing.Point(65, 18);
		this.rbS7.Name = "rbS7";
		this.rbS7.Size = new System.Drawing.Size(35, 16);
		this.rbS7.TabIndex = 1;
		this.rbS7.TabStop = true;
		this.rbS7.Text = "S7";
		this.rbS7.UseVisualStyleBackColor = true;
		this.rbS7.CheckedChanged += new System.EventHandler(RadioButton_CheckedChanged);
		this.rbModbusTcp.AutoSize = true;
		this.rbModbusTcp.Location = new System.Drawing.Point(106, 18);
		this.rbModbusTcp.Name = "rbModbusTcp";
		this.rbModbusTcp.Size = new System.Drawing.Size(83, 16);
		this.rbModbusTcp.TabIndex = 2;
		this.rbModbusTcp.TabStop = true;
		this.rbModbusTcp.Text = "Modbus Tcp";
		this.rbModbusTcp.UseVisualStyleBackColor = true;
		this.rbModbusTcp.CheckedChanged += new System.EventHandler(RadioButton_CheckedChanged);
		this.grpProtocols.Controls.Add(this.rbOPCUA);
		this.grpProtocols.Controls.Add(this.rbSocket);
		this.grpProtocols.Controls.Add(this.rbModbusRtu);
		this.grpProtocols.Controls.Add(this.rbModbusTcp);
		this.grpProtocols.Controls.Add(this.rbS7);
		this.grpProtocols.Location = new System.Drawing.Point(93, 135);
		this.grpProtocols.Name = "grpProtocols";
		this.grpProtocols.Size = new System.Drawing.Size(371, 45);
		this.grpProtocols.TabIndex = 4;
		this.grpProtocols.TabStop = false;
		this.rbSocket.AutoSize = true;
		this.rbSocket.Location = new System.Drawing.Point(282, 18);
		this.rbSocket.Name = "rbSocket";
		this.rbSocket.Size = new System.Drawing.Size(59, 16);
		this.rbSocket.TabIndex = 2;
		this.rbSocket.TabStop = true;
		this.rbSocket.Text = "Socket";
		this.rbSocket.UseVisualStyleBackColor = true;
		this.rbSocket.CheckedChanged += new System.EventHandler(RadioButton_CheckedChanged);
		this.rbModbusRtu.AutoSize = true;
		this.rbModbusRtu.Location = new System.Drawing.Point(193, 18);
		this.rbModbusRtu.Name = "rbModbusRtu";
		this.rbModbusRtu.Size = new System.Drawing.Size(83, 16);
		this.rbModbusRtu.TabIndex = 2;
		this.rbModbusRtu.TabStop = true;
		this.rbModbusRtu.Text = "Modbus Rtu";
		this.rbModbusRtu.UseVisualStyleBackColor = true;
		this.rbModbusRtu.CheckedChanged += new System.EventHandler(RadioButton_CheckedChanged);
		this.label10.AutoSize = true;
		this.label10.Location = new System.Drawing.Point(22, 144);
		this.label10.Name = "label10";
		this.label10.Size = new System.Drawing.Size(65, 12);
		this.label10.TabIndex = 2;
		this.label10.Text = "协    议：";
		this.label11.AutoSize = true;
		this.label11.ForeColor = System.Drawing.Color.Red;
		this.label11.Location = new System.Drawing.Point(220, 216);
		this.label11.Name = "label11";
		this.label11.Size = new System.Drawing.Size(17, 12);
		this.label11.TabIndex = 32;
		this.label11.Text = "* ";
		this.label12.AutoSize = true;
		this.label12.ForeColor = System.Drawing.Color.Red;
		this.label12.Location = new System.Drawing.Point(220, 189);
		this.label12.Name = "label12";
		this.label12.Size = new System.Drawing.Size(17, 12);
		this.label12.TabIndex = 33;
		this.label12.Text = "* ";
		this.txtPort.Location = new System.Drawing.Point(93, 213);
		this.txtPort.Name = "txtPort";
		this.txtPort.Size = new System.Drawing.Size(121, 21);
		this.txtPort.TabIndex = 6;
		this.txtIP.Location = new System.Drawing.Point(93, 186);
		this.txtIP.Name = "txtIP";
		this.txtIP.Size = new System.Drawing.Size(121, 21);
		this.txtIP.TabIndex = 5;
		this.label13.AutoSize = true;
		this.label13.Location = new System.Drawing.Point(52, 216);
		this.label13.Name = "label13";
		this.label13.Size = new System.Drawing.Size(35, 12);
		this.label13.TabIndex = 30;
		this.label13.Text = "Port:";
		this.label14.AutoSize = true;
		this.label14.Location = new System.Drawing.Point(64, 189);
		this.label14.Name = "label14";
		this.label14.Size = new System.Drawing.Size(23, 12);
		this.label14.TabIndex = 31;
		this.label14.Text = "IP:";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(559, 561);
		base.Controls.Add(this.label11);
		base.Controls.Add(this.label12);
		base.Controls.Add(this.txtPort);
		base.Controls.Add(this.txtIP);
		base.Controls.Add(this.label13);
		base.Controls.Add(this.label14);
		base.Controls.Add(this.grpProtocols);
		base.Controls.Add(this.grpParams);
		base.Controls.Add(this.txtDesc);
		base.Controls.Add(this.txtName);
		base.Controls.Add(this.cmbModel);
		base.Controls.Add(this.label6);
		base.Controls.Add(this.label10);
		base.Controls.Add(this.label3);
		base.Controls.Add(this.cmbBrand);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.cmbDeviceType);
		base.Controls.Add(this.label9);
		base.Controls.Add(this.label8);
		base.Controls.Add(this.label7);
		base.Controls.Add(this.label5);
		base.Controls.Add(this.label4);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.btnConfirm);
		base.Controls.Add(this.btnCancel);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.Name = "DeviceEditForm";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "DeviceEditForm";
		base.Load += new System.EventHandler(DeviceEditForm_Load);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(DeviceEditForm_KeyDown);
		this.grpParams.ResumeLayout(false);
		this.grpProtocols.ResumeLayout(false);
		this.grpProtocols.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
